package com.wlz.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 *  netty 编解码
 * @author wlz
 * @date 2022-07-09  2:06 下午
 */
@Slf4j
public class CodecNettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        log.info("从客户端读取到的string 是: {}", msg.toString());
//        log.info("从客户端读取到的object 是: {}", ((MyUser)msg).toString());

        // 测试用protostuff对对象编解码
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        log.info(" 从客户端读取到的object 是: {}", MyProtostuffUtil.deserializer(bytes, MyUser.class));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
